package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;
    /*
    * 查询当前用户登录的所有地址信息
    * */
    @GetMapping("/list")
    public Result<List<AddressBook>> getAddressBook() {
        List<AddressBook> list = addressBookService.getAddressBook();
        return Result.success(list);
    }

    /*
    * 查询默认地址
    * */
    @GetMapping("/default")
    public Result<AddressBook> getDefaultAddressBook() {
        AddressBook list = addressBookService.getDefaultAddressBook();
        return Result.success(list);
    }

    /*
    * 新增地址
    * */
    @PostMapping
    public Result<String> addAddressBook(@RequestBody AddressBook addressBook) {
        addressBookService.addAddressBook(addressBook);
        return Result.success();
    }

    /*
    * 根据地址id查询回显数据
    * */
    @GetMapping("/{id}")
    public Result<AddressBook> getAddressBookById(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getAddressBookById(id);
        return Result.success(addressBook);
    }

    /*
    * 根据地址id修改地址数
    * */
    @PutMapping
    public Result<String> updateAddressBook(@RequestBody AddressBook addressBook) {
        addressBookService.updateAddressBookById(addressBook);
        return Result.success();
    }

    /*
    * 根据地址id删除地址数
    * */
    @DeleteMapping
    public Result<String> deleteAddressBookById(@RequestParam Long id) {
        addressBookService.deleteAddressBookById(id);
        return Result.success();
    }

    /*
    * 设置默认地址
    * */
    @PutMapping("/default")
    public void updateDefaultAddressBookById(@RequestBody AddressBook addressBook) {
        addressBookService.updateDefaultAddressBookById(addressBook);
    }
}
